package com.itheima.service.impl;


import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void add(List<OrderSetting> orderSettingList) {
        orderSettingList.forEach(orderSetting -> {
            if (orderSettingDao.findOrderSettingByOrderDate(orderSetting.getOrderDate()) != null) {
                orderSettingDao.editNumberByOrderDate(orderSetting);
            } else {
                orderSettingDao.add(orderSetting);
            }
        });

    }

    @Override
    public List<Map<String, Object>> getOrderSettingByMonth(String date) {

        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(date);
        List<Map<String, Object>> mapList = new ArrayList<>();
        list.forEach(orderSetting -> {
            Map<String, Object> mapTemp = new HashMap<String, Object>();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(orderSetting.getOrderDate());

            mapTemp.put("date", calendar.get(Calendar.DAY_OF_MONTH));
            mapTemp.put("number", orderSetting.getNumber());
            mapTemp.put("reservations", orderSetting.getReservations());
            mapList.add(mapTemp);
        });
        return mapList;
    }
}
